<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Task;

use App\Model\Play;
use App\Model\PlayDatum;
use App\Utils\Enum;

class SyncMatchNormalTask extends BaseSyncTask
{
    public function execute(): void
    {
        parent::execute();
        $curId = 0;
        while (true) {
            $matchList = Play::query()->where([['id', '>', $curId], ['match_status', '<', Enum\MatchEnum::MATCH_STATUS_END]])->orderBy('id')->limit(100)->select(['id', 'match_id', 'match_status', 'match_status_id'])->get();
            if ($matchList->isEmpty()) {
                break;
            }
            foreach ($matchList as $match) {
                $this->syncInit($match['match_id']);
                $curId = $match['id'];

                $detail = $this->api->getMatchInfo($match['match_id']);
                if ($detail && $detail['code'] == '0000' && !empty($match['data']['result'])) {
                    $update = [];
                    if($detail['data']['result']['match']['matchStatus'] != $match['match_status']) {
                        $update['match_status'] = $detail['data']['result']['match']['matchStatus'];
                    }
                    if ($detail['data']['result']['match']['statusId'] != $match['match_status_id']) {
                        $update['match_status_id'] = $detail['data']['result']['match']['statusId'];
                    }
                    if (isset($detail['data']['result']['match']['spMatchStatus'])) {
                        $update['sp_match_status'] = $detail['data']['result']['match']['spMatchStatus'];
                    }
                    if (isset($update['match_status']) && $update['match_status'] == Enum\MatchEnum::MATCH_STATUS_END) {
                        $update['match_end_time'] = time();
                    }
                    if (!empty($detail)) {
                        Play::query()->where('id', $match['id'])->update($update);
                    }
                }
                $extend = [];
                $fields = [];
                //指数
                if ($this->toSync('odds')) {
                    $odds = $this->api->getMatchOdds($match['match_id']);
                    if ($odds && $odds['code'] == '0000' && !empty($odds['data']['result'])) {
                        $fields[] = '指数';
                        $extend['odds'] = new_json_encode($odds['data']['result']);
                        $this->setSyncFlag('odds');
                    }
                }

                //分析
                if ($this->toSync('analysis')) {
                    $analysis = $this->api->getMatchAnalysis($match['match_id']);
                    if ($analysis && $analysis['code'] == '0000' && !empty($analysis['data']['result'])) {
                        $fields[] = '分析';
                        $extend['analysis'] = new_json_encode($analysis['data']['result']);
                        $this->setSyncFlag('analysis');
                    }
                }

                //情报
                if ($this->toSync('news')) {
                    $news = $this->api->getMatchNews($match['match_id']);
                    if ($news && $news['code'] == '0000' && !empty($news['data'])) {
                        $fields[] = '情报';
                        $extend['news'] = new_json_encode($news['data']);
                        $this->setSyncFlag('news');
                    }
                }

                //积分榜
                if ($this->toSync('tables')) {
                    $tables = $this->api->getMatchTables($match['match_id']);
                    if ($tables && $tables['code'] == '0000' && !empty($tables['data']['result'])) {
                        $fields[] = '积分榜';
                        $extend['tables'] = new_json_encode($tables['data']['result']);
                        $this->setSyncFlag('tables');
                    }
                }

                if (!empty($extend)) {
                    $ret = PlayDatum::query()->where('match_id', $match['match_id'])->update($extend);
                    if ($ret) {
                        $this->logger->warning(sprintf('常规数据更新 (%s) %s %s VS %s [%s]', $match['match_id'], date('Y-m-d H:i', $match['match_time']), $match['home_team_name'], $match['away_team_name'], implode(' ', $fields)));
                    }
                }
            }
        }
    }
}